package com.bazaaroid.client.web.gwt.partnermodule.client.view.partnerinfo.impl;

import com.bazaaroid.client.web.gwt.partnermodule.client.dto.PartnerAddressDTO;
import com.bazaaroid.client.web.gwt.partnermodule.client.dto.PartnerContactDTO;
import com.bazaaroid.client.web.gwt.partnermodule.client.dto.PartnerInfoDTO;
import com.bazaaroid.client.web.gwt.partnermodule.client.event.partnerinfo.PartnerGenericViewHandler;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.common.LoadingPanel;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.common.TitleCommandBar;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.partnerinfo.EditPartnerAddressEntryView;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.partnerinfo.EditPartnerContactEntryView;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.partnerinfo.EditPartnerInfoEntryView;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.partnerinfo.PartnerAddressEntryView;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.partnerinfo.PartnerContactEntryView;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.partnerinfo.PartnerInfoEntryView;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.partnerinfo.PartnerInfoTabView;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class PartnerInfoTabViewImpl extends Composite implements
		PartnerInfoTabView {

	@UiTemplate("PartnerInfoTabView.ui.xml")
	interface PartnerInfoTabViewUiBinder extends
			UiBinder<Widget, PartnerInfoTabViewImpl> {
	}

	private static PartnerInfoTabViewUiBinder uiBinder = GWT
			.create(PartnerInfoTabViewUiBinder.class);

	private Presenter presenter;

	private LoadingPanel loadingPanel = new LoadingPanel(new Label("Loading"));
	private LoadingPanel savingPanel = new LoadingPanel(new Label("Loading"));
	private PartnerInfoDTO partnerInfoDTO;
	private PartnerAddressDTO partnerAddressDTO;
	private PartnerContactDTO partnerContactDTO;
	private Widget partnerInfoWidget;
	private Widget partnerAddressWidget;
	private Widget partnerContactWidget;
	private PartnerGenericViewHandler<PartnerInfoDTO> partnerInfoViewHandler;
	private PartnerGenericViewHandler<PartnerAddressDTO> partnerAddressViewHandler;
	private PartnerGenericViewHandler<PartnerContactDTO> partnerContactViewHandler;
	
	
	@UiField
	VerticalPanel partnerInfoNavigationPanel;
	@UiField
	VerticalPanel containerPanel;
	@UiField
	VerticalPanel partnerInfoPanel;
	@UiField
	VerticalPanel partnerAddressPanel;
	@UiField
	VerticalPanel partnerContactPanel;

	private class NewPartnerInfoClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			PartnerInfoDTO partnerInfoDTOTemp = new PartnerInfoDTO();
			EditPartnerInfoEntryView editPartnerInfoEntryView = new EditPartnerInfoEntryView(
					PartnerInfoTabViewImpl.this, partnerInfoDTOTemp);
			partnerInfoPanel.insert(editPartnerInfoEntryView, 0);
			partnerInfoDTO = partnerInfoDTOTemp;
			partnerInfoWidget = editPartnerInfoEntryView;
		}
	}
	
	private class NewPartnerAddressClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			PartnerAddressDTO partnerAddressDTOTemp = new PartnerAddressDTO();
			EditPartnerAddressEntryView editPartnerAddressEntryView = new EditPartnerAddressEntryView(
					PartnerInfoTabViewImpl.this, partnerAddressDTOTemp);
			partnerAddressPanel.insert(editPartnerAddressEntryView, 0);
			partnerAddressDTO = partnerAddressDTOTemp;
			partnerAddressWidget = editPartnerAddressEntryView;
		}
	}
	
	private class NewPartnerContactClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			PartnerContactDTO partnerContactDTOTemp = new PartnerContactDTO();
			EditPartnerContactEntryView editPartnerContactEntryView = new EditPartnerContactEntryView(
					PartnerInfoTabViewImpl.this, partnerContactDTOTemp);
			partnerContactPanel.insert(editPartnerContactEntryView, 0);
			partnerContactDTO = partnerContactDTOTemp;
			partnerContactWidget = editPartnerContactEntryView;
		}
	}
	
	public PartnerInfoTabViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		
		containerPanel.add(loadingPanel);
		containerPanel.add(savingPanel);

		TitleCommandBar titleBar = new TitleCommandBar("Partner Management");
		titleBar.addCommand("Add Information", new NewPartnerInfoClickHandler());
		titleBar.addCommand("Add Address", new NewPartnerAddressClickHandler());
		titleBar.addCommand("Add Contact", new NewPartnerContactClickHandler());
		containerPanel.add(titleBar);

		final PartnerInfoDTO partnerInfoDTO = new PartnerInfoDTO();
		partnerInfoDTO.setName("Springfield");
		partnerInfoDTO.setDescription("bla bla bla bla bla");
		addPartnerInfoEntry(partnerInfoDTO);
		
		final PartnerAddressDTO partnerAddressDTO = new PartnerAddressDTO();
		partnerAddressDTO.setStreet("Knez Mihajlova");
		partnerAddressDTO.setNumber("6");
		partnerAddressDTO.setCity("Beograd");
		partnerAddressDTO.setPostalCode("11000");
		partnerAddressDTO.setTownship("Beograd");
		partnerAddressDTO.setState("Srbija");
		partnerAddressDTO.setCountry("Srbija");
		addPartnerAddressEntry(partnerAddressDTO);
		
		final PartnerContactDTO partnerContactDTO = new PartnerContactDTO();
		partnerContactDTO.setTelephone("+381111234567");
		partnerContactDTO.setMobile("+381641234567");
		partnerContactDTO.setFax("+381111234568");
		partnerContactDTO.setEmail("springfield@mail.com");
		partnerContactDTO.setSite("www.springfield.com");
		addPartnerContactEntry(partnerContactDTO);
	}

	public PartnerGenericViewHandler<PartnerInfoDTO> getPartnerInfoViewHandler() {
		return partnerInfoViewHandler;
	}
	
	public PartnerGenericViewHandler<PartnerAddressDTO> getPartnerAddressViewHandler() {
		return partnerAddressViewHandler;
	}
	
	public PartnerGenericViewHandler<PartnerContactDTO> getPartnerContactViewHandler() {
		return partnerContactViewHandler;
	}
	
	public VerticalPanel getPartnerInfoNavigationPanel() {
		return partnerInfoNavigationPanel;
	}

	public LoadingPanel getLoadingPanel() {
		return loadingPanel;
	}

	public LoadingPanel getSavingPanel() {
		return savingPanel;
	}

	public void addPartnerInfoEntry(PartnerInfoDTO partnerInfoDTO) {
		PartnerInfoEntryView partnerInfoEntryView = new PartnerInfoEntryView(this, partnerInfoDTO);
		this.partnerInfoPanel.insert(partnerInfoEntryView, 0);
		this.partnerInfoDTO = partnerInfoDTO;
		this.partnerInfoWidget = partnerInfoEntryView;
	}

	public void addPartnerAddressEntry(PartnerAddressDTO partnerAddressDTO) {
		PartnerAddressEntryView partnerAddressEntryView = new PartnerAddressEntryView(this, partnerAddressDTO);
		this.partnerAddressPanel.insert(partnerAddressEntryView, 0);
		this.partnerAddressDTO = partnerAddressDTO;
		this.partnerAddressWidget = partnerAddressEntryView;
	}
	
	public void addPartnerContactEntry(PartnerContactDTO partnerContactDTO) {
		PartnerContactEntryView partnerContactEntryView = new PartnerContactEntryView(this, partnerContactDTO);
		this.partnerContactPanel.insert(partnerContactEntryView, 0);
		this.partnerContactDTO = partnerContactDTO;
		this.partnerContactWidget = partnerContactEntryView;
	}
	
	public void startEditPartnerInfo(PartnerInfoEntryView partnerInfoEntryView) {
		EditPartnerInfoEntryView editPartnerInfoEntryView = new EditPartnerInfoEntryView(partnerInfoEntryView);
		swapPartnerInfoViews(partnerInfoEntryView, editPartnerInfoEntryView, partnerInfoEntryView.getPartnerGenericDTO());
	}
	
	public void startEditPartnerAddress(PartnerAddressEntryView partnerAddressEntryView) {
		EditPartnerAddressEntryView editPartnerAddressEntryView = new EditPartnerAddressEntryView(partnerAddressEntryView);
		swapPartnerAddressViews(partnerAddressEntryView, editPartnerAddressEntryView, partnerAddressEntryView.getPartnerGenericDTO());
	}
	
	public void startEditPartnerContact(PartnerContactEntryView partnerContactEntryView) {
		EditPartnerContactEntryView editPartnerContactEntryView = new EditPartnerContactEntryView(partnerContactEntryView);
		swapPartnerContactViews(partnerContactEntryView, editPartnerContactEntryView, partnerContactEntryView.getPartnerGenericDTO());
	}
	
	public void endEditPartnerInfo(EditPartnerInfoEntryView editPartnerInfoEntryView,
			PartnerInfoEntryView partnerInfoEntryView) {
		swapPartnerInfoViews(editPartnerInfoEntryView, partnerInfoEntryView, editPartnerInfoEntryView.getPartnerGenericDTO());
	}

	public void endEditPartnerAddress(EditPartnerAddressEntryView editPartnerAddressEntryView,
			PartnerAddressEntryView partnerAddressEntryView) {
		swapPartnerAddressViews(editPartnerAddressEntryView, partnerAddressEntryView, editPartnerAddressEntryView.getPartnerGenericDTO());
	}
		
	public void endEditPartnerContact(EditPartnerContactEntryView editPartnerContactEntryView,
			PartnerContactEntryView partnerContactEntryView) {
		swapPartnerContactViews(editPartnerContactEntryView, partnerContactEntryView, editPartnerContactEntryView.getPartnerGenericDTO());
	}
	
	public void onPartnerInfoEntrySaved(
			PartnerInfoEntryView partnerInfoEntryView,
			EditPartnerInfoEntryView editPartnerInfoEntryView,
			PartnerInfoDTO partnerInfoDTO) {
		partnerInfoViewHandler.onPartnerGenericEntrySaved(partnerInfoDTO);
		if (partnerInfoEntryView != null) {
			partnerInfoEntryView.update();
			partnerInfoEntryView.getPartnerInfoTabView().endEditPartnerInfo(
					editPartnerInfoEntryView, partnerInfoEntryView);
		} else {
			addPartnerInfoEntry(partnerInfoDTO);
			editPartnerInfoEntryView.removeFromParent();
		}
	}
	
	public void onPartnerAddressEntrySaved(
			PartnerAddressEntryView partnerAddressEntryView,
			EditPartnerAddressEntryView editPartnerAddressEntryView,
			PartnerAddressDTO partnerAddressDTO) {
		partnerAddressViewHandler.onPartnerGenericEntrySaved(partnerAddressDTO);
		if (partnerAddressEntryView != null) {
			partnerAddressEntryView.update();
			partnerAddressEntryView.getPartnerInfoTabView().endEditPartnerAddress(
					editPartnerAddressEntryView, partnerAddressEntryView);
		} else {
			addPartnerAddressEntry(partnerAddressDTO);
			editPartnerAddressEntryView.removeFromParent();
		}
		
	}

	public void onPartnerContactEntrySaved(
			PartnerContactEntryView partnerContactEntryView,
			EditPartnerContactEntryView editPartnerContactEntryView,
			PartnerContactDTO partnerContactDTO) {
		partnerContactViewHandler.onPartnerGenericEntrySaved(partnerContactDTO);
		if (partnerContactEntryView != null) {
			partnerContactEntryView.update();
			partnerContactEntryView.getPartnerInfoTabView().endEditPartnerContact(
					editPartnerContactEntryView, partnerContactEntryView);
		} else {
			addPartnerContactEntry(partnerContactDTO);
			editPartnerContactEntryView.removeFromParent();
		}
		
	}
	
	public void swapPartnerInfoViews(Widget oldView, Widget newView, PartnerInfoDTO partnerInfoDTO) {
		int index = partnerInfoPanel.getWidgetIndex(oldView);
		oldView.removeFromParent();
		partnerInfoPanel.insert(newView, index);
		this.partnerInfoDTO = partnerInfoDTO;
		this.partnerInfoWidget = newView;
	}

	public void swapPartnerAddressViews(Widget oldView, Widget newView, PartnerAddressDTO partnerAddressDTO) {
		int index = partnerAddressPanel.getWidgetIndex(oldView);
		oldView.removeFromParent();
		partnerAddressPanel.insert(newView, index);
		this.partnerAddressDTO = partnerAddressDTO;
		this.partnerAddressWidget = newView;
	}
	
	public void swapPartnerContactViews(Widget oldView, Widget newView, PartnerContactDTO partnerContactDTO) {
		int index = partnerContactPanel.getWidgetIndex(oldView);
		oldView.removeFromParent();
		partnerContactPanel.insert(newView, index);
		this.partnerContactDTO = partnerContactDTO;
		this.partnerContactWidget = newView;
		
	}
	
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	public Widget asWidget() {
		return this;
	}


}
